import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from  '../components/Welcome'
import Users from '../components/user/Users'
import Roles from  '../components/Roles'


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login, },
  { path: '/home', component: Home,
    children:[
      { path: '/', redirect: '/welcome' },
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/roles', component: Roles },
      ]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  //to将要访问路径
  //from 代表从那个路劲跳转过来
  //next 是个函数 表示放行
  //next() 直接放行  next('/home') 强制跳转
  if (to.path === '/login') return next()
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
